//package com.bruce.practice.es.configuration;
//
//
//import com.bruce.practice.es.config.ElsearchConfig;
//import org.apache.commons.lang3.StringUtils;
//
//import org.apache.http.HttpHost;
//import org.apache.http.auth.AuthScope;
//import org.apache.http.auth.UsernamePasswordCredentials;
//import org.apache.http.client.CredentialsProvider;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.impl.client.BasicCredentialsProvider;
//import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import java.util.Arrays;
//import java.util.Objects;
//
///**
// * @author luotuan
// * @Description es 客户端配置
// * @create 2020-04-26 19:43
// **/
////@Configuration
//public class ElasticsearchClientConfiguration {
//    private static final Logger logger = LoggerFactory.getLogger(ElasticsearchClientConfiguration.class);
//
//    private static final int ADDRESS_LENGTH = 2;
//
//    private static final String HTTP_SCHEME = "http";
//
//    private static final String DEFAULT_SPLIT = ",";
//
//   // @Autowired
//    private ElsearchConfig elsearchConfig;
//
//    /**
//     * 高级Client
//     *
//     * @param restClientBuilder
//     * @return
//     */
//    //@Bean
//    public RestHighLevelClient restHighLevelClient(@Autowired RestClientBuilder restClientBuilder) {
//        if(StringUtils.isNotEmpty(elsearchConfig.getPassword())){
//            //初始化ES操作客户端
//            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//            credentialsProvider.setCredentials(AuthScope.ANY,
//                    new UsernamePasswordCredentials(elsearchConfig.getName(), elsearchConfig.getPassword()));  //es账号密码
//            restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
//                @Override
//                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
//                    httpAsyncClientBuilder.disableAuthCaching();
//                    return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//                }
//            });
//        }
//
//        restClientBuilder.setMaxRetryTimeoutMillis(60000);
//        return new RestHighLevelClient(restClientBuilder);
//    }
//
//    @Bean
//    public RestClientBuilder restClientBuilder() {
//        String hosts = elsearchConfig.getHosts();
//        if (hosts.contains(DEFAULT_SPLIT)) {
//            String[] hostArray = hosts.split(DEFAULT_SPLIT);
//            HttpHost[] httpPosts = Arrays.stream(hostArray)
//                    .map(this::createHttpHost)
//                    .filter(Objects::isNull)
//                    .toArray(HttpHost[]::new);
//            return RestClient.builder(httpPosts);
//        }
//        return RestClient.builder(createHttpHost(hosts));
//    }
//
//    /**
//     * 构建HttpPost
//     *
//     * @param s
//     * @return
//     */
//    private HttpHost createHttpHost(String s) {
//        assert StringUtils.isNotEmpty(s);
//        String[] address = s.split(":");
//        if (address.length == ADDRESS_LENGTH) {
//            String ip = address[0];
//            int port = Integer.parseInt(address[1]);
//            return new HttpHost(ip, port, HTTP_SCHEME);
//        } else {
//            return null;
//        }
//    }
//}
